home *** CD-ROM | disk | FTP | other *** search
/ Aminet 16 / Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso / Aminet / dev / src / wangisrc.lha / wangi / z / NaeGreybak / NaeGreyPatch.c < prev    next >
C/C++ Source or Header  |  1995-07-11  |  4KB  |  144 lines

  1. // PatchBlack.c -- Patches the PubScreenStatus() function of Intuition
  2. //
  3. // ** includes **************************************************************
  4.  
  5. #include <proto/exec.h>
  6. #include <proto/intuition.h>
  7. #include <proto/dos.h>
  8.  
  9. #include <exec/types.h>
  10. #include <exec/ports.h>
  11. #include <exec/memory.h>
  12. #include <dos/dostags.h>
  13. #include <dos/notify.h>
  14.  
  15. #include "NaeGreyPatch_rev.h"
  16.  
  17. // ** defines & types *******************************************************
  18.  
  19. #define FUNCOFFSET 0xFFFFFDD8
  20. #define REG(x) register __ ## x
  21. #define DELAY_SECS 5
  22. #define TASK_NAME "NaeGrey Patch"
  23.  
  24. typedef UWORD __asm (*MYFUNC)(REG(a0) struct Screen *, REG(d0) UWORD );
  25.  
  26. // ** globals ***************************************************************
  27.  
  28. const STRPTR version = VERSTAG;
  29. MYFUNC OldPubScreenStatus;
  30.  
  31. // ** CBACK declerations ****************************************************
  32.  
  33. long __stack = 1000;                // Amount of stack space our task needs 
  34. char *__procname = TASK_NAME;       // The name of the task to create
  35. long __priority = 0;                // The priority to run the task a
  36.  
  37. // **************************************************************************
  38. void DoCommand(void)
  39. {    
  40.     BPTR fileh;
  41.     if (fileh = Open("NIL:", MODE_OLDFILE)) {
  42.         if (SystemTags("NaeGrey ALL ON QUIET", SYS_Input,  fileh,
  43.                                                SYS_Output, 0,
  44.                                                SYS_Asynch, TRUE,
  45.                                                TAG_END) != 0) {
  46.             Close(fileh);
  47.         }
  48.     }
  49. }
  50.  
  51. // **************************************************************************
  52. UWORD __saveds __asm NewPubScreenStatus(REG(a0) struct Screen *screen, REG(d0) UWORD StatusFlags)
  53.  
  54. {
  55.     UWORD result;
  56.     result = OldPubScreenStatus(screen, StatusFlags);
  57.     if (StatusFlags == 0)
  58.         DoCommand(); 
  59.     return(result);
  60. }
  61.  
  62.  
  63. // **************************************************************************
  64. main()
  65. {
  66.     if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 36)) {
  67.         struct MsgPort *port;
  68.         BOOL patch = TRUE;
  69.         
  70.         Forbid();
  71.         if (port = FindPort(TASK_NAME)) {
  72.             struct Message *msg;
  73.             
  74.             // We are already running... tell other occurance to quit
  75.             patch = FALSE;
  76.             if (msg = (struct Message *)AllocVec(sizeof(struct Message), MEMF_CLEAR)) {
  77.                 struct MsgPort *rp;
  78.                 
  79.                 if (rp = CreateMsgPort()) {
  80.                     msg->mn_Node.ln_Type = NT_MESSAGE;
  81.                     msg->mn_ReplyPort = rp;
  82.                     msg->mn_Length = sizeof(struct Message);
  83.                     PutMsg(port, msg);
  84.                     WaitPort(rp);
  85.                     GetMsg(rp);
  86.                     FreeVec(msg);
  87.                     DeleteMsgPort(rp);
  88.                 }
  89.             }
  90.         }
  91.         Permit();
  92.             
  93.         if (patch == TRUE) {
  94.             if (port = CreateMsgPort()) {
  95.                 BYTE sig;
  96.                 
  97.                 port->mp_Node.ln_Name = TASK_NAME;
  98.                 AddPort(port);
  99.                 if ((sig = AllocSignal(-1)) != -1)  {
  100.                     ULONG ret, sigm, portm;
  101.                     BOOL cont = TRUE;
  102.                     struct NotifyRequest nr;
  103.                     
  104.                     sigm = 1L<<sig;
  105.                     portm = 1L<<port->mp_SigBit;
  106.                     nr.nr_Name = "ENV:Sys";
  107.                     nr.nr_FullName = NULL;
  108.                     nr.nr_Flags = NRF_SEND_SIGNAL;
  109.                     nr.nr_stuff.nr_Signal.nr_Task = FindTask(NULL);
  110.                     nr.nr_stuff.nr_Signal.nr_SignalNum = sig;
  111.                     nr.nr_Reserved[0] = 0;
  112.                     nr.nr_Reserved[1] = 0;
  113.                     nr.nr_Reserved[2] = 0;
  114.                     nr.nr_Reserved[3] = 0;
  115.                     StartNotify(&nr);
  116.                 
  117.                     OldPubScreenStatus = (MYFUNC)SetFunction((struct Library *)IntuitionBase, FUNCOFFSET, (ULONG (* )())&NewPubScreenStatus);
  118.                 
  119.                     while (cont == TRUE) {
  120.                         ret = Wait(SIGBREAKF_CTRL_C | sigm | portm);
  121.                         if (ret & sigm) {           // system preferences have been changed... 
  122.                             Delay(DELAY_SECS*50);
  123.                             DoCommand();
  124.                         }
  125.                         if (ret & SIGBREAKF_CTRL_C) // Control-C
  126.                             cont = FALSE;
  127.                         if (ret & portm) {          // user has run us again... quit;
  128.                             struct Message *msg;
  129.                             if (msg = GetMsg(port))
  130.                                 ReplyMsg(msg);
  131.                             cont = FALSE;
  132.                         }
  133.                     }
  134.                     SetFunction((struct Library *)IntuitionBase, FUNCOFFSET, (ULONG (* )())OldPubScreenStatus);
  135.                     EndNotify(&nr);
  136.                     FreeSignal(sig);
  137.                 }
  138.                 RemPort(port);
  139.                 DeleteMsgPort(port);
  140.             }
  141.         }    
  142.         CloseLibrary((struct Library *)IntuitionBase);
  143.     }
  144. }